import tkinter as tk
from tkinter import messagebox

# 棋盘大小
BOARD_SIZE = 8
# 每个格子的大小
GRID_SIZE = 60
# 窗口大小
WINDOW_WIDTH = GRID_SIZE * BOARD_SIZE
WINDOW_HEIGHT = GRID_SIZE * BOARD_SIZE

# 初始化棋盘
board = [[0] * BOARD_SIZE for _ in range(BOARD_SIZE)]
board[3][3] = 2
board[3][4] = 1
board[4][3] = 1
board[4][4] = 2

# 当前玩家，1 表示黑子，2 表示白子
current_player = 1

# 检查是否可以落子
def can_place(x, y, player):
    if board[x][y] != 0:
        return False
    directions = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
    for dx, dy in directions:
        i, j = x + dx, y + dy
        if 0 <= i < BOARD_SIZE and 0 <= j < BOARD_SIZE and board[i][j] == 3 - player:
            while 0 <= i < BOARD_SIZE and 0 <= j < BOARD_SIZE and board[i][j] == 3 - player:
                i += dx
                j += dy
            if 0 <= i < BOARD_SIZE and 0 <= j < BOARD_SIZE and board[i][j] == player:
                return True
    return False

# 翻转棋子
def flip_pieces(x, y, player):
    directions = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
    for dx, dy in directions:
        i, j = x + dx, y + dy
        to_flip = []
        while 0 <= i < BOARD_SIZE and 0 <= j < BOARD_SIZE and board[i][j] == 3 - player:
            to_flip.append((i, j))
            i += dx
            j += dy
        if 0 <= i < BOARD_SIZE and 0 <= j < BOARD_SIZE and board[i][j] == player:
            for fx, fy in to_flip:
                board[fx][fy] = player

# 检查是否有可落子的位置
def has_valid_move(player):
    for i in range(BOARD_SIZE):
        for j in range(BOARD_SIZE):
            if can_place(i, j, player):
                return True
    return False

# 处理鼠标点击事件
def on_click(event):
    global current_player
    x = event.x // GRID_SIZE
    y = event.y // GRID_SIZE
    if can_place(x, y, current_player):
        board[x][y] = current_player
        flip_pieces(x, y, current_player)
        draw_board()
        next_player = 3 - current_player
        if not has_valid_move(next_player):
            if not has_valid_move(current_player):
                end_game()
            else:
                messagebox.showinfo("提示", f"玩家 {next_player} 无合法落子位置，玩家 {current_player} 继续。")
        else:
            current_player = next_player

# 绘制棋盘
def draw_board():
    canvas.delete("all")
    for i in range(BOARD_SIZE):
        for j in range(BOARD_SIZE):
            canvas.create_rectangle(
                j * GRID_SIZE,
                i * GRID_SIZE,
                (j + 1) * GRID_SIZE,
                (i + 1) * GRID_SIZE,
                outline="black"
            )
            if board[i][j] == 1:
                canvas.create_oval(
                    j * GRID_SIZE + 5,
                    i * GRID_SIZE + 5,
                    (j + 1) * GRID_SIZE - 5,
                    (i + 1) * GRID_SIZE - 5,
                    fill="black"
                )
            elif board[i][j] == 2:
                canvas.create_oval(
                    j * GRID_SIZE + 5,
                    i * GRID_SIZE + 5,
                    (j + 1) * GRID_SIZE - 5,
                    (i + 1) * GRID_SIZE - 5,
                    fill="white"
                )

# 结束游戏
def end_game():
    black_count = sum(row.count(1) for row in board)
    white_count = sum(row.count(2) for row in board)
    if black_count > white_count:
        winner = "黑子"
    elif white_count > black_count:
        winner = "白子"
    else:
        winner = "平局"
    messagebox.showinfo("游戏结束", f"黑子数量: {black_count}, 白子数量: {white_count}, 结果: {winner}")
    root.destroy()

# 创建主窗口
root = tk.Tk()
root.title("黑白棋游戏")

# 创建画布
canvas = tk.Canvas(root, width=WINDOW_WIDTH, height=WINDOW_HEIGHT)
canvas.pack()

# 绘制初始棋盘
draw_board()

# 绑定鼠标点击事件
canvas.bind("<Button-1>", on_click)

# 运行主循环
root.mainloop()